package com.jobpass.controller;

import com.jobpass.common.data.ResultData;
import com.jobpass.common.service.EnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Api(tags = "处理登录")
@RequestMapping("/company")
@Controller
public class LoginController {

    @Resource
    EnterpriseService enterpriseServiceImpl;

    @ApiOperation(value = "用户登录", notes = "必须有username,password")
    @PostMapping("/login")
    private Object login(@RequestParam String username, @RequestParam String password){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户登录的数据
        UsernamePasswordToken token = new UsernamePasswordToken (username,password);
        try {
            subject.login(token);
            return "redirect:admin";  //执行登录
        }catch (UnknownAccountException e){   //用户名不存在
            return ResultData.fail("400", "用户名不存在");
        } catch (IncorrectCredentialsException e){   //密码不正确
            return ResultData.fail("400", "密码不正确");
        }
    }
}
